/**
 * 生产工厂
 *
 * @author sk.z
 */
public class Factory {

    private Stage[] stageLine;  //工序流程

    private Product[] productArr;   //产品

    public static void main(String[] args) {
        Factory factory = new Factory();

        //配置工序过程
        factory.configStageLine();
        //初始化产品
        factory.initProduct(80);

        //启动生产
        int count;
        while ((count = factory.working()) > 0) {
            System.out.println("生产加工次数:" + count);
        }

        //统计结果
        factory.showProduct();
        factory.countProducts();
        System.out.println();
        factory.showStage();
    }

    //生产引擎
    private int working() {
        int count = 0;
        //通过工序设备 获取符合前置工序产品进行生产
        for (int si = 0; si < stageLine.length; si++) {
            Stage curStage = stageLine[si];
            count += curStage.process(productArr);
        }
        return count;
    }

    private void configStageLine() {

        Stage a = new Stage(1, "S_A");
        Stage b = new Stage(2, "S_B");
        Stage c = new Stage(3, "S_C");
        Stage d = new Stage(4, "S_D");
        Stage e = new Stage(5, "S_E");
        Stage f = new Stage(6, "S_F");

        b.addPreStage(a);
        c.addPreStage(b);
        d.addPreStage(b);
        e.addPreStage(c);
        e.addPreStage(d);
        f.addPreStage(e);

        stageLine = new Stage[]{a, b, c, d, e, f};

        //set index
        for (int i = 0; i < stageLine.length; i++) {
            stageLine[i].setIndex(i);
        }

        //add machine
        Machine a1 = new Machine(1, "M_A1", 95, 100);
        Machine b1 = new Machine(1, "M_B1", 95, 90);
        Machine c1 = new Machine(1, "M_C1", 80, 30);
        Machine c2 = new Machine(1, "M_C2", 80, 30);
        Machine d1 = new Machine(1, "M_D1", 95, 100);
        Machine e1 = new Machine(1, "M_E1", 95, 70);
        Machine f1 = new Machine(1, "M_F1", 95, 100);
        a.addMachine(a1);
        b.addMachine(b1);
        c.addMachine(c1);
        c.addMachine(c2);
        d.addMachine(d1);
        e.addMachine(e1);
        f.addMachine(f1);
    }

    //number 产品个数
    private void initProduct(int number) {
        int size = stageLine.length;
        productArr = new Product[number];
        for (int i = 0; i < number; i++) {
            productArr[i] = new Product(size);
        }
    }

    private void showProduct() {
        System.out.println("产品加工详情: 1-合格 X-不合格 -未加工");
        for (int i = 0; i < productArr.length; i++) {
            System.out.print(productArr[i].toString().
                    replaceAll("2", "X").
                    replaceAll("0", "-"));
            if ((i + 1) % 5 == 0) {
                System.out.println();
            }
        }
    }

    private void showStage() {
        System.out.println("生产线情况:");
        for (Stage stage : stageLine) {
            stage.show();
        }
    }

    private void countProducts() {
        int completed = 0;
        int qualified = 0;
        for (Product p : productArr) {
            if (p.isCompleted()) {
                completed++;
                if (p.isQualified()) {
                    qualified++;
                }
            }
        }
        System.out.println("预加工产品总数:" + productArr.length + " 完成总数:" + completed + " 合格数量:" + qualified);
    }

}
